package com.sxbbc.common.basics.utils.richText;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

/**
 * html过滤
 *
 * @author Arno
 */
public class HtmlUtil {

    /**
     * 获取论坛html 内容
     *
     * @param result
     * @return
     */
    public static String getHtmlStr(String result) {
        String regxpForImgTag = "<[^>]+>";
        //只保留文字
        Pattern pattern = Pattern.compile(regxpForImgTag);
        Matcher matcher = pattern.matcher(result);
        String r = matcher.replaceAll("");
        r = clearStr(r);
        if (r.length() > 280) {
            r = r.substring(0, 280) + "...";
        }
        return r;
    }

    /**
     * 获取论坛html 内容和图片
     *
     * @param result
     * @return
     */
    public static Map<String, Object> getHtml(String result) {
        Map<String, Object> map = new HashMap<String, Object>();
        String regxpForImgTag = "<[^>]+>";
        //只保留文字
        Pattern pattern = Pattern.compile(regxpForImgTag);
        Matcher matcher = pattern.matcher(result);
        String r = matcher.replaceAll("");

        //保留只剩图片和文字
        Pattern p_style = Pattern.compile("</?(?!img|source)[^>]+>");
        Matcher m_style = p_style.matcher(result);
        result = m_style.replaceAll("");
        List<String> imgList = getImgaddress(result);
        r = clearStr(r);
        map.put("content", r);
        map.put("imgList", imgList);
        return map;
    }

    /**
     * 获取论坛html 内容和图片
     *
     * @param result
     * @return
     */
    public static List<Map<String, Object>> getHtmlList(String result) {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        Pattern p_style = Pattern.compile("</?(?!img|source)[^>]+>");
        Matcher m_style = p_style.matcher(result);
        result = m_style.replaceAll("");

        //过滤标签
        result = clearStr(result);
        String[] test = result.split("<img|<source");
        for (String s : test) {
            if (!StringUtils.isEmpty(s)) {
                s = clearStr(s);
                String[] a = s.split(">");
                if (a != null && a.length > 0) {
                    for (int i = 0; i < a.length; i++) {
                        if (!StringUtils.isEmpty(a[i])) {
                            Map<String, Object> map = new HashMap<String, Object>();
                            if (a[i].indexOf("src=\"") > -1) {
                                String img = a[i].substring(a[i].indexOf("src=\"") + 5, a[i].length() - 1);
                                String str = img.substring(0, img.lastIndexOf("."));
                                String ext = img.substring(img.lastIndexOf(".") + 1);
                                if (!"mp4".equals(ext.toLowerCase())) {
                                    str = str + "_middle" + "." + ext;
                                } else {
                                    str = str + "." + ext;
                                }
                                map.put("content", str);
                                map.put("flag", 1);
                            } else {
                                map.put("content", a[i]);
                                map.put("flag", 0);
                            }
                            list.add(map);
                        }
                    }
                }
            }
        }
        return list;
    }

    /**
     * 过滤标签
     *
     * @param result
     */
    public static String clearStr(String result) {
        //最后替换标签
        result = result.replace("&nbsp;", "");
        result = result.replace("\n", "");
        result = result.replace("\t", "");
        result = result.replace("\r", "");
        return result;
    }

    /**
     * @param s
     * @return 获得图片
     */
    public static List<String> getImg(String s) {
        String regex;
        List<String> list = new ArrayList<String>();
        regex = "src=\"(.*?)\"";
        Pattern pa = Pattern.compile(regex, Pattern.DOTALL);
        Matcher ma = pa.matcher(s);
        while (ma.find()) {
            list.add(ma.group());
        }
        return list;
    }

    /**
     * 返回存有图片地址的数组
     *
     * @param tar
     * @return
     */
    public static List<String> getImgaddress(String tar) {
        List<String> imgList = getImg(tar);
        List<String> list = new ArrayList<String>();
        if (imgList.size() > 0) {
            for (int i = 0; i < imgList.size(); i++) {
                int begin = imgList.get(i).indexOf("\"") + 1;
                list.add(imgList.get(i).substring(begin, imgList.get(i).length() - 1));
            }
        }
        return list;
    }
}
